//
//  BattleSettle.m
//  SpaceStorm
//
//  Created by  mmj on 13-7-30.
// 战斗结算  歼灭胜利
//

#import "BattleDestroyWin.h"
#import "cocos2d.h"
#import "BubbleSprite.h"
#import "SlowBlood.h"

@implementation BattleDestroyWinShipItem

+(id) itemWithIndex:(int)index
{
    return [[[self alloc] initWithIndex:index] autorelease];
}

-(id) initWithIndex:(int)index
{
    if( (self=[super init]))
    {
        //战舰下面的背景
        shipBlackBg = [CCSprite spriteWithSpriteFrameName:@"battleBlueBg.png"];
        //战舰
        shipBg = [CCSprite spriteWithSpriteFrameName:@"battleShip.png"];
        
        //线条
        shipLineBg = [CCSprite spriteWithSpriteFrameName:@"battleLineBg1.png"];
        //名称
        shipName = [CCLabelTTF labelWithString:@"奥克夕斯级" fontName:@"Marker Felt" fontSize:10];
        
        //初始化自己总战斗力槽
        _blood = [SlowBlood2Layer bloodWithBG:@"battleOutLine.png" blood:@"battleGreenLine.png"];
        
        
        
        shipBlackBg.position = ccp(shipBlackBg.contentSize.width/2, 7+shipBlackBg.contentSize.height/2);
        [self addChild:shipBlackBg];
        
        
        shipBg.position = ccp(shipBg.contentSize.width/2+12, shipBg.contentSize.height/2+14);
        [self addChild:shipBg];
        
        
        //方法－
        //        CCTexture2D *newTexture2 = [[CCTextureCache sharedTextureCache] addImage:@"battleGridBg.png"];  //可以  是白色
        // [shipBg setTexture:newTexture2];
        
        
        
        //左对齐
        shipName.anchorPoint = CGPointMake(0, 0.5);
        shipName.position = ccp(92, 21+shipName.contentSize.height/2);
        shipName.color  = ccc3(255,255, 255);
        
        [self addChild:shipName];
        
        
        _blood.position = ccp(135 + _blood.contentSize.width/2, 14+_blood.contentSize.height/2);
        [self addChild:_blood];
        
        
        shipLineBg.scaleX = 2.15;
        shipLineBg.position = ccp(50+shipLineBg.contentSize.width/2, 0);
        [self addChild:shipLineBg];
        
    }
    return self;
}

-(void) updateDestroyWinShipInfo:(int) shipType1 shipName1:(NSString* ) shipName1 bloodPercent1:(int) bloodPercent1
{
    //更换对象
    //方法二  替换成功
    //  NSString *frameName = [NSString stringWithFormat:@"battleGridBg.png"];
    //        NSString *frameName = [NSString stringWithFormat:@"%s.png","battleGridBg"];
    //    NSString *frameName = [NSString stringWithFormat:@"%d.png",11];
    //CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
    //[shipBg setDisplayFrame:frame];
    
    
    //玩家名字
    //[ppName setString:[NSString stringWithFormat:@"%@", userData.userName]];
    //等级表示
    //[ppLevel setString:[NSString stringWithFormat:@"%d", userData.level]];
    
    [shipName setString:[NSString stringWithFormat:@"%@",shipName1]];
    [_blood setProgress:bloodPercent1];
    
    
}

@end


@implementation BattleDestroyWinGoodsItem
+(id) itemWithIndex:(int)index
{
    return [[[self alloc] initWithIndex:index] autorelease];
}
-(id) initWithIndex:(int)index
{
    if( (self=[super init]))
    {
        CCSprite* goodsBlackBg;  //物品的背景
        goodsBlackBg = [CCSprite spriteWithSpriteFrameName:@"battleGridBg.png"];
        goodsBlackBg.position = ccp(goodsBlackBg.contentSize.width/2, goodsBlackBg.contentSize.height/2);
        [self addChild:goodsBlackBg];
        
        goodsBg = [CCSprite spriteWithSpriteFrameName:@"battleGoods.png"];
        goodsBg.position = ccp(goodsBg.contentSize.width/2+(goodsBlackBg.contentSize.width-goodsBg.contentSize.width)/2, goodsBg.contentSize.height/2+(goodsBlackBg.contentSize.height-goodsBg.contentSize.height)/2);
        [self addChild:goodsBg];
        
    }
    return self;
    
}


-(void) updateDestroyWinGoodsInfo:(int) goodsType
{
    //方法二  替换成功
    NSString *frameName = [NSString stringWithFormat:@"battleGridBg.png"];
    // NSString *frameName = [NSString stringWithFormat:@"%s.png","battleGridBg"];
    //   NSString *frameName = [NSString stringWithFormat:@"%d.png",11];
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
    [goodsBg setDisplayFrame:frame];
    
    
}

@end



@implementation BattleDestroyWin

@synthesize armNeed;
@synthesize powerNum;
@synthesize ourLoseList;
@synthesize enemyLoseList;
@synthesize goodsList;

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	BattleDestroyWin *layer = [BattleDestroyWin node];
    
	[scene addChild: layer];
    
	return scene;
}

-(id) init
{
    if( (self=[super init]))
    {
        
        //缓冲图片文件
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIBattleSettle_480_320.plist"];
        
        // ask director the the window size
        size = [[CCDirector sharedDirector] winSize];
        
        
        //初始化背景
        [self initBackGround];
        
        //初始化标题头
        [self initHead];
        
        //菜单
        [self initMenu];
        
        // 信息
        [self initInfo:ourLoseList enemyLoseList1:enemyLoseList goodsList1:goodsList];
        
        
        
        
        //最上层的装饰
        [self initTopDecorate];
        
        
        [self updateLabelInfo:30 powerNum:50];
    }
    //忘记  return self; 会报错
    return self;
}

/**
 初始化背景
 */
-(void) initBackGround
{
    CCSprite* backGround = [CCSprite spriteWithSpriteFrameName:@"storageBg.jpg"];
    backGround.position = ccp(size.width / 2 , size.height / 2);
    [self addChild:backGround];
    
    //左边外框
    BubbleSprite *leftOutBg = [BubbleSprite spriteWithFrameName:@"decoration.png" size:CGSizeMake(201, 202) leftCap:75 topCap:16];
    leftOutBg.position = ccp(35+leftOutBg.contentSize.width/2, size.height - 47 - leftOutBg.contentSize.height/2);
    [self addChild:leftOutBg];
    
    
    //右边外框
    BubbleSprite *rightOutBg = [BubbleSprite spriteWithFrameName:@"decoration.png" size:CGSizeMake(201, 202) leftCap:75 topCap:16];
    rightOutBg.position = ccp(size.width-rightOutBg.contentSize.width/2-35, size.height - 47 - rightOutBg.contentSize.height/2);
    //X轴 镜像反转
    [rightOutBg setFlipX:YES];
    [self addChild:rightOutBg];
    
    //线条1
    CCSprite* lineBg1 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg2.png"];
    lineBg1.position = ccp(size.width/2, size.height - 266 -lineBg1.contentSize.height/2);
    [self addChild:lineBg1];
    
    //线条2
    CCSprite* lineBg2 = [CCSprite spriteWithSpriteFrameName:@"battleLineBg2.png"];
    lineBg2.position = ccp(size.width/2, size.height - 305 - lineBg2.contentSize.height/2);
    [self addChild:lineBg2];
    
}

/**
 初始化头部
 */
-(void) initHead
{
    //标题背景
    CCSprite *headBg = [CCSprite spriteWithSpriteFrameName:@"titleBlackBg.png"];
    //标题歼灭胜利
    CCSprite *titlwineBg = [CCSprite spriteWithSpriteFrameName:@"battleTitle.png"];
    //明线条
    CCSprite *brigtLineBg = [CCSprite spriteWithSpriteFrameName:@"battleLightLine.png"];
    headBg.position = ccp((size.width/2) , (size.height - headBg.contentSize.height/2- 9));
    [self addChild:headBg];
    
    titlwineBg.position = ccp((size.width)/2 , (size.height - titlwineBg.contentSize.height/2)-12);
    [self addChild:titlwineBg];
    
    brigtLineBg.position = ccp(size.width/2, size.height - 39);
    [self addChild:brigtLineBg];
    
}

/**
 初始化菜单
 */
-(void) initMenu
{
    //战报
    CCSprite* powerNormal = [CCSprite spriteWithSpriteFrameName:@"battleBtnNormal.png"];
    CCSprite* powerClick = [CCSprite spriteWithSpriteFrameName:@"battleBtnClick.png"];
    
    //确认
    CCSprite* sureNormal = [CCSprite spriteWithSpriteFrameName:@"battleBtnNormal.png"];
    CCSprite* sureClick = [CCSprite spriteWithSpriteFrameName:@"battleBtnClick.png"];
    
    //左边箭头
    CCSprite* leftArrowNormal = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    CCSprite* leftArrowClick = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    
    //右边箭头
    CCSprite* rightArrowNormal = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    CCSprite* rightArrowClick = [CCSprite spriteWithSpriteFrameName:@"battleArrowBg.png"];
    //game menu
    CCMenuItemSprite* power;  //战报
    CCMenuItemSprite* sure;     //确认
    CCMenuItemSprite* leftArrow;     //左边箭头
    CCMenuItemSprite* rightArrow;     //右边箭头
    
    //战报
    CCSprite* battleReport = [CCSprite spriteWithSpriteFrameName:@"battleReport.png"];
    //确认
    CCSprite* battleSure = [CCSprite spriteWithSpriteFrameName:@"battleSure.png"];
    
    //X轴 镜像反转
    [powerNormal setFlipX:YES];
    [powerClick setFlipX:YES];
    power = [CCMenuItemSprite itemFromNormalSprite:powerNormal selectedSprite:powerClick target:self selector:@selector(onPowerButton:)];
    power.position = ccp(power.contentSize.width / 2 +39 , size.height - power.contentSize.height/2 -268);
    
    sure = [CCMenuItemSprite itemFromNormalSprite:sureNormal selectedSprite:sureClick target:self selector:@selector(onSureButton:)];
    sure.position = ccp(size.width -  power.contentSize.width / 2 -39 , size.height - sure.contentSize.height/2 -268);
    
    leftArrow = [CCMenuItemSprite itemFromNormalSprite:leftArrowNormal selectedSprite:leftArrowClick target:self selector:@selector(onLeftArrowButton:)];
    leftArrow.position = ccp(130 + leftArrow.contentSize.width/2 , size.height - leftArrow.contentSize.height/2 -268);
    //X轴 镜像反转
    [rightArrowNormal setFlipX:YES];
    [rightArrowClick setFlipX:YES];
    rightArrow = [CCMenuItemSprite itemFromNormalSprite:rightArrowNormal selectedSprite:rightArrowClick target:self selector:@selector(onRightArrowButton:)];
    rightArrow.position = ccp(size.width - 130 - rightArrow.contentSize.width/2 , size.height - rightArrow.contentSize.height/2 -268);
    
    //菜单
    menu = [CCMenu menuWithItems:power, sure,leftArrow, rightArrow, nil];
    menu.position = CGPointZero;
    [self addChild:menu];
    
    //战报
    battleReport.position = ccp(battleReport.contentSize.width/2+66, size.height - 278 -battleReport.contentSize.height/2);
    [self addChild:battleReport];
    //确认
    battleSure.position = ccp(size.width - 100+ battleSure.contentSize.width/2  , size.height - 278 - battleSure.contentSize.height/2);
    [self addChild:battleSure];
    
}

/**
 初始化信息
 */
-(void) initInfo:(CCArray*) ourLoseList1 enemyLoseList1:(CCArray*) enemyLoseList goodsList1:(CCArray*)goodsList1
{
    
    BattleDestroyWinShipItem *leftshipItem1;
    //我方损失列表
    int i;
    for(i = 0; i<4; i++)
    {
        leftshipItem1 = [BattleDestroyWinShipItem itemWithIndex:i];
        leftshipItem1.position = ccp(41, size.height -  99 - 47*i);
        [self addChild:leftshipItem1];
    }
    
    //敌方损失列表
    BattleDestroyWinShipItem *rightshipItem1;
    for( i = 0; i<4; i++)
    {
        rightshipItem1 = [BattleDestroyWinShipItem itemWithIndex:2];
        rightshipItem1.position = ccp(258, size.height -  99 - 47* i);
        [self addChild:rightshipItem1];
    }
    
    

    //战利品
    BattleDestroyWinGoodsItem * goodsItem1;
    for(i = 0;i<5;i++)
    {
        goodsItem1 = [BattleDestroyWinGoodsItem itemWithIndex:i];
        goodsItem1.position = ccp(147+39*i+goodsItem1.contentSize.width/2, 17-goodsItem1.contentSize.height/2);
        [self addChild:goodsItem1];
    }
    
    

    
    
    //[leftshipItem1 updateDestroyWinShipInfo:leftshipItem1 shipType1:30 shipName1:"sss" bloodPercent1:30];
    
    
    
}

-(void) updateLabelInfo:(int) armNeed powerNum:(int) powerNum
{
    
    [labelArmNeed setString:[NSString stringWithFormat:@"%d",armNeed]];
    [labelPower setString:[NSString stringWithFormat:@"%d",powerNum]];
    
    
    
    
}

/**
 初始化装饰
 */
-(void) initTopDecorate
{
    //战利品
    CCSprite* battleReard = [CCSprite spriteWithSpriteFrameName:@"battleReward.png"];
    battleReard.position = ccp(size.width/2, size.height - 257);
    [self addChild:battleReard];
    
    //军需
    CCSprite* armyNeed = [CCSprite spriteWithSpriteFrameName:@"battleArmyNeed.png"];
    armyNeed.position = ccp(299+armyNeed.contentSize.width/2, size.height - 259);
    [self addChild:armyNeed];
    //战功
    CCSprite* battlePower = [CCSprite spriteWithSpriteFrameName:@"battlePower.png"];
    battlePower.position = ccp(372+battlePower.contentSize.width/2, size.height - 259);
    [self addChild:battlePower];
    
    
    
    
    //左边花纹
    CCSprite* leftFigureBg = [CCSprite spriteWithSpriteFrameName:@"battleFigureBg.png"];
    leftFigureBg.position = ccp(leftFigureBg.contentSize.width/2 , size.height - 48-leftFigureBg.contentSize.height/2);
    [self addChild:leftFigureBg];
    
    //右边花纹
    CCSprite* rightFigureBg = [CCSprite spriteWithSpriteFrameName:@"battleFigureBg.png"];
    rightFigureBg.position = ccp(size.width - rightFigureBg.contentSize.width/2 , size.height - 48 - rightFigureBg.contentSize.height/2);
    //X轴 镜像反转
    [rightFigureBg setFlipX:YES];
    [self addChild:rightFigureBg];
    
    //我方损失
    CCSprite* ourLoseBg = [CCSprite spriteWithSpriteFrameName:@"ourLose.png"];
    ourLoseBg.position = ccp(39+ourLoseBg.contentSize.width/2, size.height - 38 -ourLoseBg.contentSize.height/2);
    [self addChild:ourLoseBg];
    
    //敌方损失
    CCSprite* enemyLoseBg = [CCSprite spriteWithSpriteFrameName:@"enemyLose.png"];
    enemyLoseBg.position = ccp(size.width - 39 - enemyLoseBg.contentSize.width/2, size.height - 38 - enemyLoseBg.contentSize.height/2);
    [self addChild:enemyLoseBg];
    
    //滚动条背景左边
    BubbleSprite *leftScrollBg = [BubbleSprite spriteWithFrameName:@"storageScrollBg.png" size:CGSizeMake(8, 154) leftCap:2 topCap:2];
    leftScrollBg.position = ccp(224 + leftScrollBg.contentSize.width/2, size.height - leftScrollBg.contentSize.height/2 - 53);
    [self addChild:leftScrollBg];
    
    //滚动条背景右边
    BubbleSprite *rightScrollBg = [BubbleSprite spriteWithFrameName:@"storageScrollBg.png" size:CGSizeMake(8, 154) leftCap:2 topCap:2];
    rightScrollBg.position = ccp(size.width-rightScrollBg.contentSize.width/2 - 224, size.height - rightScrollBg.contentSize.height/2 - 53);
    [self addChild:rightScrollBg];
    
    //军需
    labelArmNeed = [CCLabelTTF labelWithString:@"10000" fontName:@"Marker Felt" fontSize:10];
    //战功
    labelPower = [CCLabelTTF labelWithString:@"3000" fontName:@"Marker Felt" fontSize:10];
    
    //军需
    labelArmNeed.anchorPoint = CGPointMake(0, 0.5);
    labelArmNeed.position = ccp(size.width - 146, size.height - 259);
    labelArmNeed.color  = ccc3(0,255, 255);
    [self addChild:labelArmNeed];
    
    //战功
    labelPower.anchorPoint = CGPointMake(0, 0.5);
    labelPower.position = ccp(size.width - 72, size.height - 259);
    labelPower.color  = ccc3(0,255, 255);
    [self addChild:labelPower];
}

/**
 战报单击处理
 */
-(void) onPowerButton:(id)sender
{
    CCLOG(@"onPowerButton");
}

/**
 确认单击处理
 */
-(void) onSureButton:(id)sender
{
    CCLOG(@"onSureButton");
}

/**
 左边箭头 单击处理
 */
-(void) onLeftArrowButton:(id)sender
{
    CCLOG(@"左边箭头");
}

/**
 右边箭头 单击处理
 */
-(void) onRightArrowButton:(id)sender
{
    CCLOG(@"右边箭头");
}

/**
 删除
 */
-(void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
    //我方损失列表
    if(self.ourLoseList)
    {
        [self.ourLoseList release];
    }
    //敌方损失列表
    if(self.enemyLoseList)
    {
        [self.enemyLoseList release];
    }
    //战利品列表
    if(self.goodsList)
    {
        [self.goodsList release];
    }
    
    
    
}

@end
